/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.rest;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.anniversary.domain.Activities;
import me.zhengjie.modules.anniversary.service.ActivitiesService;
import me.zhengjie.modules.anniversary.service.dto.ActivitiesQueryCriteria;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @author Jiayin Wei
* @date 2020-07-28
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "校庆活动管理")
@RequestMapping("/api/activities")
public class ActivitiesController {

    private final ActivitiesService activitiesService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('activities:list')")
    public void download(HttpServletResponse response, ActivitiesQueryCriteria criteria) throws IOException {
        activitiesService.download(activitiesService.queryAll(criteria), response);
    }


    @GetMapping
    @Log("查询校庆活动")
    @ApiOperation("查询校庆活动")
    //@PreAuthorize("@el.check('activities:list')")
    @AnonymousAccess
    public ResponseEntity<Object> query(ActivitiesQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(activitiesService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    /*查询用户参加的校庆活动*/
    @Log("查询校庆活动")
    @ApiOperation("查询校庆活动活动人数")
    @GetMapping("/join")
    //@PreAuthorize("@el.check('user:list','dept:list')")
    @AnonymousAccess
    public ResponseEntity<Object> getActivitiesByUserId(Long userId,Integer page,Integer size) {
        List<Map<String,Object>> activities = activitiesService.getActivitiesByUserId(userId, PageRequest.of(page, size));
        System.out.println(activities);
        return new ResponseEntity<>(activities,HttpStatus.OK);
    }

    /*根据活动id查询校庆活动*/
    @Log("查询校庆活动活动人数")
    @ApiOperation("查询校庆活动活动人数")
    @GetMapping("/byid")
    //@PreAuthorize("@el.check('user:list','dept:list')")
    @AnonymousAccess
    public ResponseEntity<Object> getActivitiesById(Long activitiesId) {
        List<Map<String,Object>> number = activitiesService.getActivitiesById(activitiesId);
        System.out.println(number);
        return new ResponseEntity<>(number,HttpStatus.OK);
    }

    /*查询校庆活动*/
    @Log("查询校庆活动活动人数")
    @ApiOperation("查询校庆活动活动人数")
    @GetMapping("/entity")
    //@PreAuthorize("@el.check('user:list','dept:list')")
    @AnonymousAccess
    public ResponseEntity<Object> getActivities(Integer page,Integer size) {
        List<Map<String,Object>> activities = activitiesService.getActivities(PageRequest.of(page, size));
        System.out.println(activities);
        return new ResponseEntity<>(activities,HttpStatus.OK);
    }

    /*模糊查询校庆活动*/
    @Log("查询校庆活动活动人数")
    @ApiOperation("查询校庆活动活动人数")
    @GetMapping("/byname")
    //@PreAuthorize("@el.check('user:list','dept:list')")
    @AnonymousAccess
    public ResponseEntity<Object> getActivitiesByname(String name,Integer page,Integer size) {
        List<Map<String,Object>> activities = activitiesService.getActivitiesByname(name,PageRequest.of(page, size));
        System.out.println(activities);
        return new ResponseEntity<>(activities,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增校庆活动")
    @ApiOperation("新增校庆活动")
    @PreAuthorize("@el.check('activities:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Activities resources){
        return new ResponseEntity<>(activitiesService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改校庆活动")
    @ApiOperation("修改校庆活动")
    @PreAuthorize("@el.check('activities:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Activities resources){
        activitiesService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除校庆活动")
    @ApiOperation("删除校庆活动")
    @PreAuthorize("@el.check('activities:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        activitiesService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("查询首页最新校庆活动信息")
    @ApiOperation("查询首页最新校庆活动信息")
    @GetMapping("/findByActivities")
//@PreAuthorize("@el.check('user:list','dept:list')")
    @AnonymousAccess
    public ResponseEntity<Object> findByActivities(Integer page,Integer size) {
        List<Map<String,Object>> activities = activitiesService.findByActivities(PageRequest.of(page, size));
        System.out.println(activities);
        return new ResponseEntity<>(activities,HttpStatus.OK);
    }

    @GetMapping(value = "/allActivityOne",produces = "text/html;charset=utf-8")
    @Log("查询校友会最新活动详情")
    @ApiOperation("查询校友会最新活动详情")
    //@PreAuthorize("@el.check('activities:list')")
    @AnonymousAccess
    public ResponseEntity<Object> queryActivityNewsOne(@RequestParam Long id){
        return new ResponseEntity<>(JSON.toJSONString(activitiesService.queryActivityNewsOne(id)), HttpStatus.OK);
    }

}
